package com.calendar.backend;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.calendar.widget.R;

public class Help extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.help_layout);

		this.setTitle("Help");

		GestureLibrary store = GestureLibraries.fromRawResource(this,
				R.raw.gestures);
		ArrayList<SearchResults> results = new ArrayList<SearchResults>();
		int i = 0;
		if (store.load()) {
			for (String name : store.getGestureEntries()) {
				if (name.equals("Add1") || name.equals("Month")
						|| name.equals("Help") || name.equals("Agenda")
						|| name.equals("Next") || name.equals("Previous"))
					for (Gesture gesture : store.getGestures(name)) {
						Bitmap bitmap = gesture.toBitmap(80, 80, 0,
								Color.YELLOW);
						results.add(addItemToArray(name, bitmap));
						i++;
					}
			}
		}

		android.util.Log.i("Calendar", "Results size: " + results.size());

		final ListView lv1 = (ListView) findViewById(R.id.ListView1);
		lv1.setAdapter(new MyCustomBaseAdapter(this, results));
	}

	private SearchResults addItemToArray(String name, Bitmap bitmap) {

		SearchResults sr1 = new SearchResults();
		sr1.setName(name);
		if (name.equals("Edit")) {
			sr1.setName("Edit Event");
			sr1.setDescription("Only usable when looking at a single event");
		} else if (name.equals("Add1")) {
			sr1.setName("Add Event");
			sr1
					.setDescription("Go to the add event view (draw vertical line first)");
		} else if (name.equals("Month")) {
			sr1.setName("Month View");
			sr1.setDescription("Go to the month view");
		} else if (name.equals("Next")) {
			sr1.setName("Next Day/Month");
			sr1.setDescription("Swipe from right to left");
		} else if (name.equals("Previous")) {
			sr1.setName("Previous Day/Month");
			sr1.setDescription("Swipe from left to right");
		} else if (name.equals("Agenda")) {
			sr1.setName("Agenda View");
			sr1.setDescription("Go to today's agenda view");
		} else {
			sr1.setDescription("");
		}
		sr1.setIcon(bitmap);
		return sr1;
	}

	public class SearchResults {
		private String name = "";
		private String description = "";
		private Bitmap icon;

		public void setName(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getDescription() {
			return description;
		}

		public void setIcon(Bitmap icon) {
			this.icon = icon;
		}

		public Bitmap getIcon() {
			return icon;
		}
	}

	public class MyCustomBaseAdapter extends BaseAdapter {
		private ArrayList<SearchResults> searchArrayList;

		private LayoutInflater mInflater;

		public MyCustomBaseAdapter(Context context,
				ArrayList<SearchResults> results) {
			searchArrayList = results;
			mInflater = LayoutInflater.from(context);
		}

		public int getCount() {
			return searchArrayList.size();
		}

		public Object getItem(int position) {
			return searchArrayList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.help_list_row, null);
				holder = new ViewHolder();
				holder.txtName = (TextView) convertView
						.findViewById(R.id.HeadingText);
				holder.txtDescription = (TextView) convertView
						.findViewById(R.id.AboutText);
				holder.imgIcon = (ImageView) convertView
						.findViewById(R.id.HelpIcon);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.txtName.setText(searchArrayList.get(position).getName());
			holder.txtDescription.setText(searchArrayList.get(position)
					.getDescription());
			holder.imgIcon.setScaleType(ScaleType.FIT_CENTER);
			holder.imgIcon.setImageBitmap(searchArrayList.get(position)
					.getIcon());

			return convertView;
		}
	}

	static class ViewHolder {
		TextView txtName;
		TextView txtDescription;
		ImageView imgIcon;
	}

}
